<?php
/**
*	@author SignpostMarv Martin
*	@package UHU
*/

/**
*	If the UHU installation root wasn't defined before uhu.php was brought into scope, an Exception is thrown.
*/
if(defined('UHU_v5_2_INSTALL') === false)
{
	throw new RuntimeException('UHU_v5_2_INSTALL not defined.');
}
/**
*	If the UHU library location wasn't defined before uhu.php was brought into scope, an Exception is thrown.
*/
if(defined('UHU_LIBS') === false)
{
	throw new RuntimeException('UHU_LIBS not defined.');
}
/**
*	If the UHU installation root wasn't defined before uhu.php was brought into scope, it is set to a default of UHU_ROOT . "/apps"
*/
if(defined('UHU_v5_2_APPS') === false)
{
	define('UHU_v5_2_APPS',UHU_v5_2_INSTALL . '/apps');
}
/**
*	If the UHU config file path hasn't been defined, set it to the install root with the filename of "uhu-config.php"
*/
if(defined('UHU_v5_2_CONFIG') === false)
{
	define('UHU_v5_2_CONFIG',UHU_v5_2_INSTALL . '/uhu-config.php');
}

/**
*	If the UHU standalone components path hasn't been defined, set it to a subdirectory of the install root
*/
if(defined('UHU_v5_2_STANDALONE') === false)
{
	define('UHU_v5_2_STANDALONE',UHU_v5_2_INSTALL . '/standalone');
}
/**
*	If UHU_ERROR_AS_EXCEPTION has not been defined, set it to true.
*/
if(defined('UHU_ERROR_AS_EXCEPTION') === false)
{
	define('UHU_ERROR_AS_EXCEPTION',true);
}




/**
*	@package UHU
**/
class uhu_v5_2
{
/**
*	Loads the configuration for the UHU isntallation, adds the UHU library path to the PHP include path.
*	@uses uhu_v5_2::$path_to_config
*	@uses uhu_v5_2::$path_to_libs
*	@uses UHU_v5_2_CONFIG
*	@uses UHU_v5_2_INSTALL
*	@see UHU_v5_2_CONFIG
*/
	public static function initialise()
	{
		static $initialised = false;
		if($initialised === false)
		{
/**
*	if UHU_DEBUG_MODE is defined, PHP errors are displayed in the script output
*/
			switch(defined('UHU_DEBUG_MODE'))
			{
				case false:
					ini_set('display_errors','0');
				break;
				default:
					ini_set('display_errors','1');
				break;
			}
/**
*	turns magic_quotes_gpc off
*/
			ini_set('magic_quotes_gpc',false);
/**
*	turns magic_quotes_runtime off
**/
			ini_set('magic_quotes_runtime',false);
/**
*	Set errors to be thrown as Exceptions if specified
*/
			if(defined('UHU_ERROR_AS_EXCEPTION'))
			{
				if(UHU_ERROR_AS_EXCEPTION === true)
				{
					set_error_handler('uhu_v5_2::error');
				}
				else if(UHU_ERROR_AS_EXCEPTION !== FALSE)
				{
					set_error_handler('uhu_v5_2::error',UHU_ERROR_AS_EXCEPTION);
				}
			}
			if(file_exists(UHU_v5_2_CONFIG))
			{
				require_once(UHU_v5_2_CONFIG);
			}
			set_include_path(implode(PATH_SEPARATOR,
				array(
					UHU_LIBS,
					UHU_v5_2_STANDALONE,
					UHU_v5_2_APPS,
					get_include_path()
				)
			));
			self::load_dir_config(UHU_v5_2_STANDALONE);
		}
		$initialised = true;
	}
/**
*	Loads the UHU Config file for the directory path if the uhu-config.php file is found in the supplied path.
*	@param string directory path to the directory for the UHU Config file
*	@return void
*/
	final public static function load_dir_config($directory)
	{
		if(is_string($directory) === false)
		{
			throw new InvalidArgumentException('Directory name must be specified as string.');
		}
		else if(is_dir($directory) === false)
		{
			throw new InvalidArgumentException('\'' . $directory . '\' is not a directory.');
		}
		else if(file_exists($directory . '/uhu-config.php'))
		{
			try
			{
				require_once($directory . '/uhu-config.php');
			}
			catch(Exception $e)
			{
				throw new RuntimeException('Could not open config file: ' . get_class($e),$e->getCode());
			}
		}
	}
/**
*	@param int $errno Error number
*	@param string $errstr Error message
*	@param string|null $errfile The file the error occured in
*	@param integer|null $errline The line the error occured on
*	@param array|null $errcontext The backtrace showing what led to the error occuring
*	@uses ErrorException::__construct
*/
	final public static function error($errno,$errstr,$errfile=null,$errline=null)
	{
		$throw = false;
		switch($errno)
		{
			case 2:
				$throw = ((UHU_ERROR_AS_EXCEPTION === true) || (UHU_ERROR_AS_EXCEPTION & E_WARNING));
			break;
			default:
				$throw = true;
			break;
		}		
		if($throw){ throw new ErrorException($errstr,0,$errno,$errfile,$errline); } # alteration due to supressed errors not meant to get thrown
	}
/**
*	Default Exception handler for UHU
*/
	public static function Exception_handler(Exception $e)
	{
		try
		{
			$ob_status = ob_get_status();
			if(empty($ob_status) === false)
			{
				ob_clean();
			}
		}
		catch(Exception $e)
		{}
		header('Content-Type:text/plain');
		header('HTTP/1.1 500 Internal Server Error');
		$error = 'There seems to be a problem in showing you the page you requested.' . "\n" .
		'If the problem persists, please contact the site owner, citing the following:' . "\n\n" .
		get_class($e) . ' error code ' . (int)$e->getCode();
		if($e instanceof PDOException){}
		if(defined('UHU_DEBUG_MODE') && UHU_DEBUG_MODE === true)
		{
			$error .= "\n" . (string)$e;
		}
		if(function_exists('UHU_output') === true)
		{
			UHU_output($error,30);
		}
		else
		{
			die($error);
		}
	}
	public static function _server()
	{
		return UHU_v5_2_server::getInstance();
	}
}
uhu_v5_2::initialise();
?>